<template>
	<view class="page">
		<view class="search-block">
			<view class="search-ico-wapper">
				<image src="../../static/icos/search.png" class="search-ico"></image>
			</view>
			<input 
				type="text" 
				placeholder="搜索预告" 
				maxlength="10" 
				class="search-text" 
				confirm-type="search"
				@confirm="searchMe"
				:focus="true" 
			/>
		</view>
		<view class="page-block movie-list">
			<view class="movie-wapper" v-for="(trailer,tIndex) in trailerList" :key="trailer.id">
				<image 
					:src="trailer.cover" 
					:data-trailerId="trailer.id"
					@click="showTrailer"
					class="poster"
				></image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				trailerList:[],
				// 搜索关键字
				keywords:"",
				// 当前页数
				page:1,
				// 总页数
				totalPages:1,
			}
		},
		onLoad() {
			// https://www.imovietrailer.com/superhero/search/list?qq=lee12141967&keywords=&page=&pageSize=
			var me = this;
			
			// 请求qq
			uni.request({
				url: 'https://www.imovietrailer.com/superhero/sys/switches',
				method: 'POST',
				complete: function(res) {
					me.qq = res.data.ok;
					// 调用开始
					me.pagedTrailerList("",1,15)
				}
			});
		},
		// 下拉触底
		onReachBottom() {
			var me = this;
			var page = me.page + 1;
			var keywords = me.keywords;
			var totalPages = me.totalPages;
			if(page > totalPages){
				return
			}
			me.pagedTrailerList(keywords,page,15)
		},
		methods: {
			showTrailer(e){
				var trailerId = e.currentTarget.dataset.trailerid;
				uni.navigateTo({
					url:"../movie/movie?trailerId=" + trailerId
				})
			},
			pagedTrailerList(keywords,page,pageSize){
				var me = this;
				
				uni.showLoading({
					mask:true,
					title:"请稍后",
				})
				
				uni.showNavigationBarLoading()
				
				// 请求猜你喜欢数据
				uni.request({
					url: me.serverUrl + "/search/list?qq=" + me.qq + "&keywords=" + keywords + "&page=" + page + "&pageSize=" + pageSize,
					method:"POST",
					success:function(res){
						console.log(res.data)
						if(res.data.status == 200){
							var tempList = res.data.data.rows;
							me.trailerList = me.trailerList.concat(tempList);
							me.totalPages = res.data.data.total;
							me.page = page;		// 覆盖当前页面
						}else{
							console.log(res.data.msg)
						}
					},
					complete:function(){
						uni.hideNavigationBarLoading()
						uni.hideLoading()
					}
				})
			},
			searchMe(e){
				var me = this;
				// 获取搜索的内容
				var value = e.detail.value
				me.keywords = value
				me.trailerList = [];
				me.pagedTrailerList(value,1,15)
			},
		}
	}
</script>

<style>
	@import url("search.css");
</style>
